home *** CD-ROM | disk | FTP | other *** search
/ Transactor / Transactor_23_1988_Transactor_Publishing.d64 / brk trap.pal (.txt) < prev    next >
Commodore BASIC  |  2023-02-26  |  4KB  |  153 lines

  1. 10 sys700
  2. 1000 ;********************************
  3. 1010 ;*                              *
  4. 1020 ;*  simulating a trap with brk  *
  5. 1030 ;*  --------------------------  *
  6. 1040 ;*                              *
  7. 1050 ;*                              *
  8. 1060 ;* brk vector is diverted so    *
  9. 1070 ;* that "invisible" subroutines *
  10. 1080 ;* can be called.               *
  11. 1090 ;*                              *
  12. 1100 ;*                              *
  13. 1110 ;*  - by tom hughes  v022287 -  *
  14. 1120 ;*                              *
  15. 1130 ;********************************
  16. 1140 ;
  17. 1150 ;c64 equates
  18. 1160 ;
  19. 1170 cbinv = $0316 ;brk vector (2)
  20. 1180 chrout = $ffd2 ;output a char
  21. 1190 clrchn = $ffcc ;i/o to defaults
  22. 1200 getin = $ffe4 ;input a char
  23. 1210 memory = $8d ;temp storage (2)
  24. 1220 oldbrk = $8b ;storage for standard brk (2)
  25. 1230 stack = $0100 ;65xx stack location
  26. 1240 tidyup = $febc ;recover from interrupt
  27. 1250 *= $c000 ;sys 32768
  28. 1260 ;------------------------------
  29. 1270 ;set brk vector to our routine
  30. 1280 ;------------------------------
  31. 1290 ;in actual use, this would be a subroutine
  32. 1300 ;called once to divert the brk vector.
  33. 1310 ;
  34. 1320 jsr clrchn
  35. 1330 sei ;disable interrupts
  36. 1340 ldx cbinv
  37. 1350 ldy cbinv+1
  38. 1360 stx oldbrk ;save the old brk vector
  39. 1370 sty oldbrk+1
  40. 1380 ldx #<newbrk ;then set new vector
  41. 1390 ldy #>newbrk
  42. 1400 stx cbinv
  43. 1410 sty cbinv+1
  44. 1420 cli ;enable interrupts
  45. 1430 ;
  46. 1440 ;------------------------------
  47. 1450 ;demo brk handler
  48. 1460 ;------------------------------
  49. 1470 ;
  50. 1480 ;this is just an example of how you
  51. 1490 ;would use brk from within a program
  52. 1500 ;
  53. 1510 demo5 ldy #0
  54. 1520 demo10 lda prompt,y ;print "number?"
  55. 1530 beq demo20
  56. 1540 jsr chrout
  57. 1550 iny
  58. 1560 bne demo10
  59. 1570 demo20 jsr getin ;check the keyboard
  60. 1580 cmp #3 ;(if stop key, quit)
  61. 1590 beq quit
  62. 1600 cmp #"1" ;for numbers 1 thru 3
  63. 1610 bcc demo20
  64. 1620 cmp #"4"
  65. 1630 bcs demo20
  66. 1640 jsr chrout
  67. 1650 and #%00001111 ;make # hex 1 - 3
  68. 1660 sta trpnm ;save in our own program
  69. 1670 lda #13 ;print a carriage gosub
  70. 1680 jsr chrout
  71. 1690 brk ;execute trap
  72. 1700 trpnm .byt 0 ;(trap #)
  73. 1710 jmp demo5 ;after brk, prg continues here
  74. 1720 ;
  75. 1730 quit sei
  76. 1740 ldx oldbrk
  77. 1750 ldy oldbrk+1
  78. 1760 stx cbinv
  79. 1770 sty cbinv+1
  80. 1780 cli
  81. 1790 rts ;back to basic
  82. 1800 ;
  83. 1810 prompt .byt 13,13
  84. 1820 .asc "number (1 - 3)? "
  85. 1830 .byt 0
  86. 1840 ;==============================
  87. 1850 ;new brk routine
  88. 1860 ;==============================
  89. 1870 ;
  90. 1880 ;entry (1) interrupts disabled (except nmi)
  91. 1890 ;           so jiffy clock is off.
  92. 1900 ;
  93. 1910 ;(2) on entry stack looks like this...
  94. 1920 ;   (assuming old sp was at $f6)
  95. 1930 ;
  96. 1940 ;         $01f6      <- old sp
  97. 1950 ;         $01f5  pch     (stack+6)
  98. 1960 ;         $01f4  pcl     (stack+5)
  99. 1970 ;         $01f3   sr     (stack+4)
  100. 1980 ;         $01f2   .a     (stack+3)
  101. 1990 ;         $01f1   .x     (stack+2)
  102. 2000 ;         $01f0   .y     (stack+1)
  103. 2010 ;         $01ef      <- current sp
  104. 2020 ;
  105. 2030 ;(3) expects trap # after brk
  106. 2040 ; (this location can be found by
  107. 2050 ; using the pc saved on stack -1.)
  108. 2060 ;
  109. 2070 newbrk tsx ;get current sp to .x
  110. 2080 lda stack+6,x ;to find pc-high
  111. 2090 sta memory+1
  112. 2100 lda stack+5,x ;and pc-low on the stack
  113. 2110 sta memory ;save this address
  114. 2120 bne new10 ;and subtract -1 from it
  115. 2130 dec memory+1 ;so we can locate trap #
  116. 2140 new10 dec memory
  117. 2150 ldy #0
  118. 2160 lda (memory),y ;get trap #
  119. 2170 tay ;adjust it so 1-3
  120. 2180 dey ;is now 0-2
  121. 2190 tya
  122. 2200 asl a ;multiply this # by 2
  123. 2210 tay
  124. 2220 lda table,y ;and use it to look up
  125. 2230 sta memory ;trap addresses
  126. 2240 iny
  127. 2250 lda table,y
  128. 2260 sta memory+1
  129. 2270 jmp (memory) ;(NULL) to a trap routine
  130. 2280 ;
  131. 2290 ;trap addresses
  132. 2300 ;
  133. 2310 table .word trap1
  134. 2320 .word trap2
  135. 2330 .word trap3
  136. 2340 ;
  137. 2350 ;//////////////////////////////
  138. 2360 ;demo trap routines
  139. 2370 ;//////////////////////////////
  140. 2380 ;
  141. 2390 trap1 lda #"1"
  142. 2400 jsr chrout
  143. 2410 jmp tidyup ;must end with this
  144. 2420 ;
  145. 2430 trap2 lda #"2"
  146. 2440 jsr chrout
  147. 2450 jmp tidyup
  148. 2460 ;
  149. 2470 trap3 lda #"3"
  150. 2480 jsr chrout
  151. 2490 jmp tidyup
  152. 2500 .end
  153.